package com.work.music.dao;

import com.work.music.entity.Comment;
import java.util.List;

import com.work.music.entity.MusicCommentManager;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

@Repository
public interface CommentMapper {
    @Delete({
        "delete from music_comment",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into music_comment (upid, typeid, ",
        "type, uid, addtime, ",
        "addip, content)",
        "values (#{upid,jdbcType=BIGINT}, #{typeid,jdbcType=BIGINT}, ",
        "#{type,jdbcType=INTEGER}, #{uid,jdbcType=BIGINT}, #{addtime,jdbcType=TIMESTAMP}, ",
        "#{addip,jdbcType=VARCHAR}, #{content,jdbcType=LONGVARCHAR})"
    })
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Long.class)
    int insert(Comment record);

    @Select({
        "select",
        "id, upid, typeid, type, uid, addtime, addip, content",
        "from music_comment",
        "where id = #{id,jdbcType=BIGINT}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="upid", property="upid", jdbcType=JdbcType.BIGINT),
        @Result(column="typeid", property="typeid", jdbcType=JdbcType.BIGINT),
        @Result(column="type", property="type", jdbcType=JdbcType.INTEGER),
        @Result(column="uid", property="uid", jdbcType=JdbcType.BIGINT),
        @Result(column="addtime", property="addtime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="addip", property="addip", jdbcType=JdbcType.VARCHAR),
        @Result(column="content", property="content", jdbcType=JdbcType.LONGVARCHAR)
    })
    Comment selectByPrimaryKey(Long id);

    @Select({
        "select",
        "id, upid, typeid, type, uid, addtime, addip, content",
        "from music_comment"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="upid", property="upid", jdbcType=JdbcType.BIGINT),
        @Result(column="typeid", property="typeid", jdbcType=JdbcType.BIGINT),
        @Result(column="type", property="type", jdbcType=JdbcType.INTEGER),
        @Result(column="uid", property="uid", jdbcType=JdbcType.BIGINT),
        @Result(column="addtime", property="addtime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="addip", property="addip", jdbcType=JdbcType.VARCHAR),
        @Result(column="content", property="content", jdbcType=JdbcType.LONGVARCHAR)
    })
    List<Comment> selectAll();

    @Update({
        "update music_comment",
        "set upid = #{upid,jdbcType=BIGINT},",
          "typeid = #{typeid,jdbcType=BIGINT},",
          "type = #{type,jdbcType=INTEGER},",
          "uid = #{uid,jdbcType=BIGINT},",
          "addtime = #{addtime,jdbcType=TIMESTAMP},",
          "addip = #{addip,jdbcType=VARCHAR},",
          "content = #{content,jdbcType=LONGVARCHAR}",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(Comment record);

    /**
     * 获取评论列表信息
     * @param typeid
     * @param page
     * @param count
     * @return
     */
    @Select({
            "select",
            "id, upid, typeid, type, uid, addtime, addip, content",
            "from music_comment",
            "where typeid = #{typeid,jdbcType=BIGINT}",
            "order by addtime desc limit #{page},#{count}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="upid", property="upid", jdbcType=JdbcType.BIGINT),
            @Result(column="typeid", property="typeid", jdbcType=JdbcType.BIGINT),
            @Result(column="type", property="type", jdbcType=JdbcType.INTEGER),
            @Result(column="uid", property="musicUser", jdbcType=JdbcType.BIGINT,
                    many = @Many(select = "com.work.music.dao.MusicUserMapper.selectByPrimaryKey")
            ),
            @Result(column="addtime", property="addtime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="addip", property="addip", jdbcType=JdbcType.VARCHAR),
            @Result(column="content", property="content", jdbcType=JdbcType.LONGVARCHAR)
    })
    List<MusicCommentManager> selectByTypeAndTypeId(@Param("typeid") long typeid,
                                                    @Param("page") int page, @Param("count") int count);
}